<?php

namespace teach\controllers;

use teach\components\WeChat;
use teach\service\Product;
use teach\util\helper\ResponseHelper;
use Yii;
use yii\filters\VerbFilter;
use yii\web\Controller;

class ProductController extends Controller
{
    public $enableCsrfValidation = false;

    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::class,
                'actions' => [
                    '*' => ['post'],
                ],
            ],
        ];
    }

    /**
     * 产品列表
     * @return \yii\web\Response
     */
    public function actionList()
    {
        $request = Yii::$app->request;
        $mid = $request->post('mid');
        $cid = $request->post('cid');
        $title = $request->post('title');

        $msg = '';
        $flag = false;
        if (empty(isset($mid))) {
            $msg = 'mid 商家id不能为空';
        } else {
            $flag = true;
        }

        if (!$flag) {
            $jsonData = ResponseHelper::buildJsonData(400, $msg);
        } else {
            $page = $request->post('page');
            $page = $page ? $page : 1;

            if ($cid ==9 ) {
                $map = " status = 1 AND is_up = 1 ";
            } else {
                $map = " status = 1 AND is_up = 1 AND  mid = {$mid} ";
            }
            if ($cid) {
                $map .= " AND cid = " . $cid;
            }

            if ($title) {
                //做过滤
                $weChat = new WeChat();
                $ret = $weChat->textShan($title);
                if ($ret["data"]) {
                    $data = json_decode($ret["data"], true);
                    if ($data["Response"]["EvilTokens"] != []) {
                        if ($data["Response"]["EvilTokens"][0]["EvilFlag"] != 0) {
                            $jsonData = ResponseHelper::buildJsonData(400, "关键词违规");
                            return $this->asJson($jsonData);
                        }
                    }
                }
                $map .= " AND title like '%{$title}%' ";
            }

            $products = new Product();
            $productList = $products->productList($map, $page, $pre_page = 10);

            $jsonData = ResponseHelper::buildJsonData(200, '成功', $productList);
        }

        return $this->asJson($jsonData);
    }

    /**
     * 产品详情
     * @return \yii\web\Response
     */
    public function actionDetail()
    {
        //TODO 传用户uid 商家mid 如果不同则返回false
        $request = Yii::$app->request;
        $pid = $request->post('pid');
        $uid = $request->post('uid');
        $mid = $request->post('mid');

        $msg = '';
        $flag = false;
        if (!$pid) {
            $msg = 'pid 商品id不能为空';
        } else {
            $flag = true;
        }

        if (!$flag) {
            $jsonData = ResponseHelper::buildJsonData(400, $msg);
        } else {
            $products = new Product();
            $mapProduct = " status = 1 AND id = {$pid}";
            $mapSku = " pid = {$pid} AND status = 1";
            $product = $products->productDetail($mapProduct, $mapSku);

            if ($product) {
                //查找用户
                if ($product['aid'] != 1) {
                    //$jsonData = ResponseHelper::buildJsonData(200, '成功', $product);
                    if ($product['mid'] == $mid) {
                        $jsonData = ResponseHelper::buildJsonData(200, '成功', $product);
                    } else {
                        $jsonData = ResponseHelper::buildJsonData(400, '您不参与此商家的商品购买');
                    }
                } else {
                    $jsonData = ResponseHelper::buildJsonData(200, '成功', $product);
                }


            } else {
                $jsonData = ResponseHelper::buildJsonData(400, '产品发生错误,联系商家');
            }
        }

        return $this->asJson($jsonData);
    }

    /**
     * 生成产品详细的二维码
     */
    public function actionQrcode()
    {
        $request = Yii::$app->request;
        $pid = $request->post('pid');
        $uid = $request->post('uid');
        $mid = $request->post('mid');

        $msg = '';
        $flag = false;

        if (!$pid) {
            $msg= '产品id不能为空';
        } elseif (!$uid) {
            $msg = '用户id不能为空';
        } elseif (!$mid) {
            $msg = '商家id不能为空';
        } else {
            $flag = true;
        }

        if (!$flag) {
            $jsonData = ResponseHelper::buildJsonData(400, $msg);
        } else {
            $WeChat = new WeChat();
            $dir = "teach/web/QRCode/{$mid}/products/";
            $scene = "mid={$mid}&user={$uid}&pid={$pid}";
            $page = "shopPage/shoppages/index/index";
            $QRCode = $WeChat->qrCode($dir, $scene, $page);
            $path = Yii::$app->params['QRCodeUrl'] . $dir . $QRCode;
            $jsonData = ResponseHelper::buildJsonData(200, '成功', $path);
        }
        return $this->asJson($jsonData);
    }
}